package com.example.lostandfoundsystem.web.middleware;

import com.example.lostandfoundsystem.services.common.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Objects;

/**
 * @author lucifer
 * @since 2024-07-26-17:28
 */

// 基础配置拦截器
public class BaseMiddleware implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 标记一些基础配置
        Objects.requireNonNull(RequestContextHolder.getRequestAttributes()).setAttribute("",request.getRemoteAddr(), RequestAttributes.SCOPE_REQUEST);
        //System.out.println("handler:"+handler.getClass().getName());
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 删除线程
        UserService.remove();
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
